package com.xauto.pioneer.appuser.util;

import android.annotation.SuppressLint;
import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-1-3
 * To change this template use File | Settings | File Templates.
 */
public class TimeUtil {

    public static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm");
    public static SimpleDateFormat sdf1=new SimpleDateFormat("dd/MM/yyyy");
    public static SimpleDateFormat sdf2=new SimpleDateFormat("HH:mm");//24小时制
    public static SimpleDateFormat sdf3=new SimpleDateFormat("yyyy-MM-dd");

//    /**
//     * 将时间戳转为字符串 "yyyy年MM月dd日HH时mm分ss秒"
//     *
//     * @param lon
//     * @param model
//     * @return
//     */
//    public static String getStrTime(Long lon, String model) {
//        // "yyyy年MM月dd日HH时mm分ss秒"
//        String re_StrTime = null;
//        SimpleDateFormat sdf = new SimpleDateFormat(model);
//        // 例如：cc_time=1291778220
//        // long lcc_time = Long.valueOf(cc_time);
//        re_StrTime = sdf.format(new Date(lon));
//        return re_StrTime;
//    }

    /** 转换时间格式String-long */
    @SuppressLint("SimpleDateFormat")
    public static long getLongTime(String time) {
        long timeString;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = formatter.parse(time);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        timeString = date.getTime();
        return timeString;
    }

    /***
     * 根据时间判断是今年的第几周
     * @param time "2016-02-26"
     * @return
     */
    public int getWeekForYear(String time) {
//        String today = "2010-01-11";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        Log.e("TimeUtil", calendar.get(Calendar.WEEK_OF_YEAR) + "");
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 判断当前日期是星期几<br>
     * <br>
     * @param pTime 修要判断的时间<br>
     * @return dayForWeek 判断结果<br>
     * @Exception 发生异常<br>
     */
    public static int dayForWeek(String pTime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(format.parse(pTime));
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }

    /** 当前时间是否是周五周六周天*/
    public static boolean isCanSubmitWeekly() {
        boolean isCanSubmit = false;
        try {
            int week = dayForWeek(getStrTime(System.currentTimeMillis(), "yyy-MM-dd"));
            Log.e("TimeUtil", "当前星期->" + week);
            if(week == 5 || week == 6 || week == 7) {
                isCanSubmit = true;
            } else {
                isCanSubmit = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isCanSubmit;
    }

    /**
     * 将时间戳转为字符串
     *
     * @param lon
     * @param model:"yyyy年MM月dd日HH时mm分ss秒"
     * @return String
     */
    @SuppressLint("SimpleDateFormat") public static String getStrTime(Long lon, String model) {
//        String re_StrTime;
//        SimpleDateFormat sdf = new SimpleDateFormat(model);
//        re_StrTime = sdf.format(new Date(lon));

//        String re_StrTime = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(lon * 1000));
//        String re_StrTime = new SimpleDateFormat(model).format(new Date(lon));
        String re_StrTime = new SimpleDateFormat(model).format(new Date(lon * 1000));

        return re_StrTime;
    }

    /**
     * 获取当前时间(12:00)
     * @return
     */
    public static String getFormatTime(){
        Date d = new Date();
        return sdf2.format(d)+"|"+sdf3.format(d);
    }


    /**
     * 格式化当前当前时间(12:00)
     * @return
     */
    public static String getFormatCurrentTime(SimpleDateFormat simpleDateFormat){
        Date d = new Date();
        return simpleDateFormat.format(d);
    }


    /**
     * 计算前后天数
     * @param simpleDateFormat
     * @param day
     * @return
     */
    public static String calculateTime(SimpleDateFormat simpleDateFormat,int day){
        Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
        ca.setTime(new Date()); // 设置时间为当前时间
        ca.add(Calendar.DATE, day);// 日期+180
        Date resultDate = ca.getTime(); // 结果
        return simpleDateFormat.format(resultDate);
    }

    /**
     * @return
     */
    public static long getToday10Time(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 22);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Log.e("TimeUtil", "今天十点->" + calendar.getTimeInMillis());
        return calendar.getTimeInMillis();
    }

    /**
     * @return
     */
    public static long getTomorrow8Time(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Log.e("TimeUtil", "明天八点->" + calendar.getTimeInMillis());
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getCurrentTime(){
        long currentTime = System.currentTimeMillis();
        return String.valueOf(currentTime);
    }

    public static String typeConvert(String[] typeCodeArray ,String[] typeNameArray ,
                                     String content){

            int length = typeNameArray.length;
            for (int i=0;i<length;i++){
                if(typeNameArray[i].equals(content)){
                    return typeCodeArray[i];
                }
            }
        return "";
    }
}
